<template>
	<view>
		<view class="VerticalBox">
			<scroll-view class="VerticalNav nav" scroll-y scroll-with-animation :scroll-top="verticalNavTop" style="height:calc(100vh)">
				<view class="cu-item" :class="item.id==tabCur?'main-color cur':''" v-for="(item,index) in list" :key="index" @tap="tabSelect":data-id="item.id">
					{{item.name}}
				</view>
			</scroll-view>
			<scroll-view class="verticalMain" scroll-y scroll-with-animation style="height:calc(100vh)" :scroll-into-view="'main-'+mainCur" @scroll="verticalMain">
				<view class="padding-top padding-lr" v-for="(item,index) in list" :key="index" :id="'main-'+item.id">
					<view class="cu-bar solid-bottom bg-white">
						<image :src="item.url+'_310_120'" mode="widthFix"></image>
					</view>
					<view class="cu-list menu-avatar">
						<view class="cu-item" v-for="(child,subIndex) in item.children" :key="subIndex" @click="searchProduct(child.id,child.name)">
							<view class="cu-avatar radius lg" :style="'background-image:url('+child.url+'_96_96);'"></view>
							<view class="content">
								<view class="text-grey">{{child.name}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {CategoryService} from "@/api/ProductService.js"
	export default {
		data() {
			return {
				list: [],tabCur: 0,mainCur: 0,verticalNavTop: 0
			};
		},
		onShow() {
			this.loadCategories();
		},
		onPullDownRefresh() {
			this.loadCategories();
			uni.stopPullDownRefresh();
		},
		methods: {
			loadCategories(){
				CategoryService.getCategories().then(res=>{
					this.list=res;
					if(this.list.length>0){
						this.listCur = this.list[0];
						this.tabCur=this.list[0].id;
					}
				});
			},
			searchProduct(categoryId,categoryName){
				uni.navigateTo({
					url:"/pages/product/list?cId="+categoryId+"&cName="+categoryName
				});
			},
			tabSelect(e) {
				this.tabCur = e.currentTarget.dataset.id;
				this.mainCur = e.currentTarget.dataset.id;
				this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50
			},
			verticalMain(e) {
				let that = this;
				let tabHeight = 0;
				for (let i = 0; i < this.list.length; i++) {
					let view = uni.createSelectorQuery().select("#main-" + this.list[i].id);
					view.fields({
						size: true
					}, data => {
						this.list[i].top = tabHeight;
						tabHeight = tabHeight + data.height;
						this.list[i].bottom = tabHeight;
					}).exec();
				}
				let scrollTop = e.detail.scrollTop + 10;
				for (let i = 0; i < this.list.length; i++) {
					if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
						this.verticalNavTop = (this.list[i].id - 1) * 50
						this.tabCur = this.list[i].id
						return false
					}
				}
			}
		},
	}
</script>

<style>
	.fixed {
		position: fixed;
		z-index: 99;
	}
	.VerticalNav.nav {
		width: 200rpx;
		white-space: initial;
	}

	.VerticalNav.nav .cu-item {
		width: 150rpx;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
	}

	.VerticalNav.nav .cu-item.cur {
		background-color: #f1f1f1;
	}

	.VerticalNav.nav .cu-item.cur::after {
		content: "";
		width: 8upx;
		height: 30upx;
		border-radius: 10upx 0 0 10upx;
		position: absolute;
		background-color: currentColor;
		top: 0;
		right: 0upx;
		bottom: 0;
		margin: auto;
	}

	.VerticalBox {
		display: flex;
	}

	.verticalMain {
		background-color: #f1f1f1;
		flex: 1;
	}
</style>
